package com.yuwei.home

import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.youth.banner.indicator.CircleIndicator
import com.yuwei.base.BaseFragment
import com.yuwei.common.entity.BannerEntity
import com.yuwei.common.entity.MenuEntity
import com.yuwei.common.path.RouterPath
import com.yuwei.home.adapter.HomeMenuAdapter
import com.yuwei.home.adapter.HomeNoticeAdapter
import com.yuwei.home.databinding.FragmentHomeBinding
import com.yuwei.home.parking_space_management.HomeBannerAdapter

@Route(path = RouterPath.MODULE_HOME_FRAGMENT)
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    var pageIndex = 0
    val homeNoticeAdapter = HomeNoticeAdapter()
    val bannerList = mutableListOf(
        BannerEntity(0, R.drawable.home_top_image),
        BannerEntity(0, R.drawable.zh1),
        BannerEntity(0, R.drawable.zh2)
    )
    private val textBannerList = listOf<String>("你好说的是饭卡手动阀", "jdskfsdjflks", "上帝就发是考虑到房价")
    private val list = mutableListOf<MenuEntity>().apply {
        // 第一行
        add(MenuEntity(R.drawable.home_menu_image1, "车位管理"))      // 粉色停车标识
        add(MenuEntity(R.drawable.home_menu_image2, "访客登记"))     // 蓝底白字登记牌
        add(MenuEntity(R.drawable.home_menu_image3, "物业报修"))     // 橙色工具图标
        add(MenuEntity(R.drawable.home_menu_image4, "考勤打卡")) // 时钟图标

        // 第二行
        add(MenuEntity(R.drawable.home_menu_image5, "房源信息"))       // 带绿树的房屋
        add(MenuEntity(R.drawable.home_menu_image6, "商家推荐"))      // 店铺图案
        add(MenuEntity(R.drawable.home_menu_image7, "月卡会员"))        // VIP字样卡片
        add(MenuEntity(R.drawable.home_menu_image8, "设备管理"))    // 带百分比的齿轮
    }
    private val homeMenuAdapter = HomeMenuAdapter()

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        binding.tvbSearch.run {
            setDatas(textBannerList)
        }
        binding.tvbBanner3.run {
            addBannerLifecycleObserver(this@HomeFragment.viewLifecycleOwner)
            setAdapter(HomeBannerAdapter(bannerList))
            setIndicator(CircleIndicator(this@HomeFragment.requireContext()))
        }
        homeMenuAdapter.data.clear()
        binding.homeRv.adapter = homeNoticeAdapter
        binding.homeRv.layoutManager = LinearLayoutManager(context)
        viewModel.getNotice(0, 10)
        binding.nsv.run {
            setOnScrollChangeListener { v, _, scrolly, _, _ ->
                if (scrolly == (binding.llt.measuredHeight - v.measuredHeight)) {
                    loadData()
                }

            }
        }

        binding.homeMenuRv.adapter = homeMenuAdapter
        binding.homeMenuRv.layoutManager = GridLayoutManager(requireContext(), 4)
        homeMenuAdapter.setOnItemClickListener { adapter, view, position ->
            if (position == 0) {
                ARouter.getInstance().build(RouterPath.PARKING_SPACE_MANAGEMENT).navigation()
            } else if (position == 1) {
                ARouter.getInstance().build(RouterPath.APPOINTMENTS_URL).navigation()
            } else if (position == 2) {
                ARouter.getInstance().build(RouterPath.ORDERS).navigation()
            } else if (position == 3) {
                ARouter.getInstance().build(RouterPath.ATTENDANCE).navigation()
            } else if (position == 4) {
                ARouter.getInstance().build(RouterPath.PROP).navigation()
            } else if (position == 5) {
                ARouter.getInstance().build(RouterPath.ENTERPRISE).navigation()
            } else if (position == 6) {
                ARouter.getInstance().build(RouterPath.YK_MODULE).navigation()
            } else if (position == 7) {
                ARouter.getInstance().build(RouterPath.DEVICES).navigation()
            }
        }
    }

    override fun initData() {
        homeMenuAdapter.data.addAll(list)
        homeMenuAdapter.notifyDataSetChanged()

        viewModel.getNoticeSuccess.observe(this) {

            homeNoticeAdapter.data.addAll(  it.filter {item ->
                item.content.isNotEmpty()
                        && item.title.isNotEmpty()
            }.reversed())
            homeNoticeAdapter.notifyDataSetChanged()

        }
    }

    fun loadData() {
        pageIndex += 1
        viewModel.getNotice(pageIndex, 5)
    }
}